package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.domain.Message;
import cn.wolfcode.trip.base.query.MessageQuery;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IMessageService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * MessageController class
 *
 * @author lishuanwang
 * @date 2019/5/31
 */
@RestController
@RequestMapping("messages")
public class MessageController {

    @Autowired
    private IMessageService messageService;

    /**
     * 通过消息作者id以及接受者id查找到信息
     * @param :消息作者id
     * @param :接受者id
     * 资源:messages/{authorId}/{reveiverId}
     * 方式:get
     * 参数:
     * 返回
     */
    @GetMapping("{authorId}/{receiverId}")
    public PageInfo<Map> listMessage(MessageQuery qo){
        return messageService.listAll(qo);
    }

    /**
     * 更新未读成为已读的状态,并且添加读取时间
     * 资源:messages/{authorId}/{receiverId}
     * 方法:put
     * 参数:
     * 返回:无
     */
    @PutMapping("{authorId}/{receiverId}")
    public void updateMessageRead(MessageQuery qo){
        messageService.updateMessageRead(qo);
    }
    /**
     * 获取该用户在与另一用户未读的消息
     * 资源:messages/authorId}/{receiverId}/unreadNum
     * 方式:get
     * 参数:MessageQuery qo
     * 返回:json,未读的条数
     */
    @GetMapping("{authorId}/{receiverId}/unreadNum")
    public Integer getUnreadNum(MessageQuery qo){
        return messageService.getUnReadMessageNums(qo);
    }
    /**
     * 获取一个用户的所有未读消息
     * 资源:messages/authorId/unreadNum
     * 方式:get
     * 参数:MessageQuery
     * 返回:json,未读消息条数
     */
    @GetMapping("{receiverId}/unreadNum")
    public Integer getUserUnReadSum(MessageQuery qo){
        return messageService.getUserUnReadMessageNums(qo);
    }

    @ResponseBody
    @GetMapping("groups/{groupId}/messages")
    public PageInfo<List<Map>> getGroupMessage(@PathVariable Long groupId, QueryObject qo){
        return  messageService.getChatRoomMessage(groupId,qo);
    }


}
